<?php

namespace app\admin\controller;

use think\Controller;
use my\Redis;
use my\Auth;

class Base extends Controller
{
    function initialize()
    {
        parent::initialize();

        // 获取uid
        $uid = cookie('op_admin_uid');

        // 免登陆检测
        $redis = new Redis();
        $redisKey = $redis::get('uid_' . $uid . '_key');
        $cookieKey = cookie('uid_' . $uid . '_key');

        if (!session('uid_' . $uid)) {
            if (empty($redisKey) || empty($cookieKey) || ($redisKey != $cookieKey)) {
                // 跳转到登录界面
                $this->error('请先登录,token过期!', '/login');
            } else {
                $this->error('请先登录!', '/login');
            }
        }


        // 控制器名和方法名
        $controllerName = $this->request->controller();
        $actionName = $this->request->action();
        $name = $controllerName . '/' . $actionName;


        $auth = new Auth();

        // 白名单
        $accessArr = ['Index/index', 'Admin/logout', 'Admin/noauth'];

        if (session('uid_' . $uid . '_name') != 'admin') { // 超级管理员什么都可以访问
            if (!in_array($name, $accessArr)) {
                if (!$auth->check($name, $uid)) {
                    $this->error('没有权限访问!', '/index');
                }
            }
        }

    }

}
